<template>
	<view class="columnbox content">
		<view class="info-title">商品推荐单</view>
		<view class="columnbox ewm-box">
			<view>请患者用微信扫描二维码</view>
			<view>或点击底部按钮发送给患者</view>
			<image class="ewm-img" :src="ewmurl" mode="widthFix"></image>
		</view>
		<view class="columnbox btn-box wdh-100">
			<view class="rowbox send-btn" @click="sendWxUser">发送给好友</view>
			<view class="rowbox send-btn" @click="sendUser">发送至患者</view>
		</view>
	</view>
</template>

<script>
	// 环信即时通讯
	import SDK from 'js_sdk/easemob-im-uni-app/Easemob-chat';
	// 实例化 SDK 对象
	const WebIM = wx.WebIM = SDK;
	export default {
		data() {
			return {
				ewmurl: '',
				id: '',
				memberid: '',
				userid: '',
				connectcount: 0,
				name: ''
			};
		},
		onLoad(options) {
			this.ewmurl = options.url
			this.id = options.data * 1
		},
		onShow() {
			if (this.memberid != '') {
				this.bindOrder()
				this.sendMessage(this.id + '', 6)
			}
		},
		methods: {
			bindOrder() {
				this.$req.post('/user/goodorderbd', {
					id: this.id * 1,
					userid: this.userid * 1
				}).then(res => {})
			},
			sendMessage(content, classify) {

				let infocontent = {
					classify: classify,
					content: '商品推荐',
					otherid: parseInt(content), //开方ID
					name: this.name,
					chatroomheadpic: 'https://zhongyi.jiayixunfang.cn/upload/logo.png',
				}
				let option = {
					type: "txt",
					msg: JSON.stringify(infocontent),
					to: this.memberid + "",
					chatType: "singleChat",
				};
				let msg = WebIM.message.create(option);
				getApp().globalData.conn.send(msg).then(res => {
					console.log("=========Send message success");
					// uni.hideLoading()
					this.$store.dispatch('actionsHandleNewChatChange', {
						msg: JSON.stringify(infocontent),
						from: this.memberid + '',
						time: Date.parse(new Date),
						chatdetailflag: true,
						serverMsgId: res.serverMsgId
					});
					setTimeout(() => {
						uni.showToast({
							title: '发送成功'
						})
						uni.hideLoading()
						uni.redirectTo({
							url: '/pages/chat/detail?id=' + this.memberid
						})
					}, 500)
				}).catch((e) => {
					console.log("==============Send message fail", e);
				});

				// uni.showLoading({
				// 	title: '正在发送'
				// })
				// getApp().globalData.chatSocket.send({
				// 	data: JSON.stringify({
				// 		"receiveid": this.memberid,
				// 		"content": content,
				// 		"classify": classify,
				// 		// "nickname": this.user.name,
				// 		// "logo": this.user.logo
				// 	}),
				// 	success: res => {
				// 		console.log('发送成功', res)
				// 		setTimeout(() => {
				// 			uni.showToast({
				// 				title: '发送成功'
				// 			})
				// 			uni.hideLoading()
				// 			uni.redirectTo({
				// 				url: '/pages/chat/detail?id=' + this.memberid
				// 			})
				// 		}, 500)
				// 	},
				// 	fail: (err) => {
				// 		console.log('发送失败', err)
				// 		this.reConnect(content, classify)
				// 	}
				// });
			},
			reConnect(content, classify) {
				setTimeout(() => {
					if (this.connectcount < 10) {
						this.connectcount++
						this.sendMessage(content, classify)
					} else {
						uni.hideLoading()
						this.connectcount = 0
						uni.showToast({
							title: '发送消息失败',
							icon: 'none'
						})
					}
				}, 500)
			},
			sendUser() {
				uni.navigateTo({
					url: '/packageA/yaofang/choosePatient?from=goods'
				})
			},
			sendWxUser() {
				wx.downloadFile({
					url: this.ewmurl,
					success: (res) => {
						wx.showShareImageMenu({
							path: res.tempFilePath
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		font-family: PingFang SC;
		height: 100vh;
		background: linear-gradient(to bottom, #1a5cde, #b6c8f4);
		justify-content: flex-start;
	}

	.info-title {
		margin-top: 100rpx;
		color: #fff;
		font-size: 40rpx;
	}

	.ewm-box {
		background: #fff;
		border-radius: 20rpx;
		width: 600rpx;
		padding: 40rpx 0;
		font-size: 28rpx;
		color: #929495;
		line-height: 40rpx;
		margin-top: 40rpx;

		.ewm-img {
			width: 500rpx;
			height: 500rpx;
			margin-top: 20rpx;
		}
	}

	.btn-box {
		position: fixed;
		bottom: 70rpx;
		left: 0;
	}

	.send-btn {
		width: 600rpx;
		background: #1e76ff;
		height: 90rpx;
		margin-bottom: 20rpx;
	}
</style>